<?php defined('SYSPATH') OR die('No direct access allowed.');

class PrimaryKey_Plugin extends WB_FormItem {
	
	protected $edit_url, $trash_url, $untrash_url, $delete_url;
	protected $edit_alt, $trash_alt, $untrash_alt, $delete_alt;
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params);
		
		$this->table_class = 'id';
		
		$base_url = isset($params['base_url']) ? $params['base_url'] : Kohana::instance()->uri->segment(1);
		$this->edit_url = isset($params['edit_url']) ? $params['edit_url'] : $base_url.'/edit/';
		$this->trash_url = isset($params['trash_url']) ? $params['trash_url'] : $base_url.'/trash/';
		$this->untrash_url = isset($params['untrash_url']) ? $params['untrash_url'] : $base_url.'/untrash/';
		$this->delete_url = isset($params['delete_url']) ? $params['delete_url'] : $base_url.'/delete/';
		
		$this->edit_alt = Kohana::lang('table.edit');
		$this->trash_alt = Kohana::lang('table.trash');
		$this->untrash_alt = Kohana::lang('table.untrash');
		$this->delete_alt = Kohana::lang('table.delete');
		
		$this->db_columns = null;
	}
	
	/**
	 * Defaults to no validation!!!
	 * 
	 * @return null
	 */
	public function prep_db()
	{
		return null;
	}
	
	public function get_display_name()
	{
		return isset($this->params['display_name']) ? $this->params['display_name'] : Kohana::lang('table.id');
	}

	
	/**
	 * Renders the form element
	 * 
	 * @return string
	 */
	public function render_item()
	{
		return null;
	}
	
	/**
	 * Returns html value for a table cell
	 * 
	 * @param mixed $value
	 * @return string
	 */
	public function render_cell()
	{
		$wb_db = Kohana::instance()->wb_db;
		$wb_template = Kohana::instance()->wb_template;
		
		$html = '<span id="id_'.$this->value.'">';
		
		if ($wb_db->get_editable() || $wb_db->get_trashable() || $wb_db->get_deletable())
		{
			
			if ($wb_db->get_editable() && ! $wb_db->get_in_trash())
				$html .= html::img_anchor($this->edit_url.$this->value, $wb_template->get_filename('img/edit.png'), $this->edit_alt);
			
			if ($wb_db->get_trashable() && ! $wb_db->get_in_trash())
				$html .= html::img_anchor($this->trash_url.$this->value, $wb_template->get_filename('img/trash.png'), $this->trash_alt);
			elseif ($wb_db->get_trashable() && $wb_db->get_in_trash())
				$html .= html::img_anchor($this->untrash_url.$this->value, $wb_template->get_filename('img/untrash.png'), $this->untrash_alt);
			
			if ($wb_db->get_deletable() && ( ! $wb_db->get_trashable() || $wb_db->get_in_trash()))
				$html .= html::img_anchor($this->delete_url.$this->value, $wb_template->get_filename('img/delete.png'), $this->delete_alt);
			
		}
		else $html .= $this->value;
		
		return $html.'</span>';
	}
	
}

//End of PrimaryKey_Plugin Library